package sisi.status;

import java.util.List;

import javax.persistence.EntityManager;

import org.zkoss.zul.Messagebox;


/**
 * Statuscontroller.java - Controller class for the <i>Status table</i>. 
 * @author Pedro Gonzalez pedro@sisisoftware.it
 * @author Claudio Pelosi claudio@sisisoftware.it
 * @author SISI Software S.n.c. - www.sisisoftware.it
 * @version 1.0, 17/03/2014
 * 
 */

public class StatusController{

	private EntityManager getEntityManager() {
		return new sisi.MyEmf().getEntityManager();
	}

	public Status[] get(String cerca) {
		List<Status> lista = getList(cerca);
			return lista.toArray(new Status[0]);
		}

	@SuppressWarnings("unchecked")
	public List<Status> getList(String cerca) {

		String cWhere = "";
		if (cerca != null && !cerca.isEmpty()) {
			cWhere = " WHERE TRIM( BOTH FROM UPPER(c.description) ) LIKE :cerca ";
		}

		EntityManager em = getEntityManager();
		try {
			javax.persistence.Query q = em
				.createQuery("select c from Status as c " + cWhere 
					+ " order by c.seq ");

			if (cerca != null && !cerca.isEmpty()) {
				q.setParameter("cerca", '%'+cerca.trim().toUpperCase()+'%' );
			}

			List<Status> lista = q.getResultList();
			return lista;
		} finally {
			em.close();
		}
	}

	public Status getxId(int id) {

		EntityManager em = getEntityManager();
		try {
			Status statusx = em.find(Status.class, id);
			return statusx;
		} finally {
			em.close();
		}
	}
	
	public Status getXCodice(String codCerca, boolean lEcho) {
		Object ar1 = null;

		EntityManager em = getEntityManager();
		try {
			
			codCerca = codCerca.toUpperCase().trim();
			
			javax.persistence.Query q = em
					.createQuery("select c from Status as c where UPPER(c.status)='"
							+ codCerca + "'");
			ar1 = q.getSingleResult();
			return (Status) ar1;
		} catch (Exception e) {
			if (lEcho){
					Messagebox.show("Status not found!",
							"Information", Messagebox.OK, Messagebox.INFORMATION);
			}
			Status ar2 = new Status();
			ar2.setDescription("");
			ar2.setSeq(null);
			ar2.setStatus("");
			return ar2;
		} finally {
			em.close();
		}
	}

	@SuppressWarnings("finally")
	public Status add(Status status) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			em.persist(status);
			em.getTransaction().commit();
			status = refresh(status);
		} finally {
			em.close();
			return status;
		}
	}

	@SuppressWarnings("finally")
	public boolean update(Status status) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			Status statusx = em.find(Status.class, status.getSeq());
			statusx.setDescription(status.getDescription());
			statusx.setSeq(status.getSeq());
			statusx.setStatus(status.getStatus());
			em.getTransaction().commit();
			em.flush();

		} finally {
			em.close();
		return false;
		}
	}

@SuppressWarnings("finally")
public Status refresh(Status status) {

		Status statusx = status;
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			statusx = em.find(Status.class, status.getSeq());
			em.flush();
			em.refresh(statusx);
		} finally {
			em.close();
			return statusx;
		}
}

@SuppressWarnings("finally")
public boolean remove(Status status) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			Status statusx = em.find(Status.class, status.getSeq());
			em.remove(statusx);
			em.getTransaction().commit();
		} finally {
			em.close();
			return false;
		}
}

}
